<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
      <el-breadcrumb-item>所有菜单</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
          <el-col :span="12">
          <!-- 搜索区域 -->
          <el-input placeholder="请输入Path" v-model="query.path" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getMenus"></el-button>
          </el-input>
        </el-col> 
       
      </el-row>

      <!-- 列表区域 -->
    <el-table
    :data="menusList"
    style="width: 100%"
    :default-sort = "sort"
    >
    <el-table-column  prop="ID"   label="ID"   sortable   width="180"> </el-table-column>
    <el-table-column  prop="parentId"   label="父ID"   sortable   width="180"> </el-table-column>
    <el-table-column  prop="path"   label="path"   sortable   width="180"> </el-table-column>
    <el-table-column  prop="name"   label="菜单名称"   sortable   width="180"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
              <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="setMenu(scope.row)"></el-button>
            <!-- 设置菜单 -->
            <el-button type="primary" icon="el-icon-setting" size="mini" @click="updateMenu(scope.row)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delMenu(scope.row)"></el-button>
          </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[ 5, 8, 15]"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>

    <!-- 添加菜单的对话框><-->
    <el-dialog
        title="添加菜单"
        :visible.sync="dialogVisible"
        width="30%"
        >
        <!-- 主体区域-->
        <el-form ref="addMenuformRef" :model="addMenuform" :rules="addFormRules" label-width="80px">
            <el-form-item label="父ID" prop="parentId">
              <el-select v-model="addMenuform.parentId" placeholder="请选择路径" :disabled="disabled">
                <el-option label="同级菜单" :value="fid"></el-option>
                <el-option label="子菜单" :value="cid"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="Path" prop="path">
              <el-input v-model="addMenuform.path"></el-input>
            </el-form-item>
            <el-form-item label="菜单名" prop="name">
              <el-input v-model="addMenuform.name"></el-input>
            </el-form-item>
        </el-form>
        <!-- 底部区域-->
        <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMenu">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>
<script >
export default {
  name: "menus",
 data() {
    return {
          menusList:[],
          total:0,
          dialogVisible:false,
          query:{
              page:1,
              pageSize:5,
              path:'',
          },
          sort:{
            prop: 'ID',
            order: 'ascending'
          },
          addMenuform:{
              name:'',
              parentId:'',
              path:'',
              id:null,
          },
          addFormRules:{
              name: [
                  { required: true, message: '请输入菜单名', trigger: 'blur' }
                ],
              parentId : [
                  { required: true, message: '请输入父ID', trigger: 'blur' }
                  ],
              path: [
                  { required: true, message: '请输入PATH', trigger: 'blur' }
                  
                ]
          },
          //父级id
          fid:0,
          //自己id
          cid:0,
          //是否可以修改
          disabled:false
      }
  },
  created() {
    this.getMenus();
  },
  mounted(){
   
  },
  methods: {
    async getMenus() {
      //get 参数  await this.$http.get("/stu/GetMenuList",{params:this.query })
      const { data: res } = await this.$http.post("/stu/GetMenuList",this.query);
      if (res.code !== 0) return this.$message.error("获取菜单列表失败");
      this.menusList = res.data.data;
      this.total=res.data.total
      console.log(this.menusList)
    },
    //监听size 改变的事件 
    handleSizeChange(newSize){
      console.log(newSize)
      this.query.pageSize=newSize
      this.getMenus()
    },
    //监听页码指改变的事件
    handleCurrentChange(newPage){
      console.log(newPage)
      this.query.page=newPage
      this.getMenus()
    },
    setMenu(row){
      this.addMenuform.id=null
      this.disabled=false
      this.cid=row.ID+''
      this.fid =row.parentId
      this.dialogVisible=true
    },
    addMenu(){
      //效验参数是否有效哦
      this.$refs.addMenuformRef.validate(async (valid)=>{
          if (!valid){ //无效
              return
          }
          var res 
          console.log(this.addMenuform)
          if (this.addMenuform.id==null){
            const {data:result}=await this.$http.post("/stu/addMenu",this.addMenuform)
            res=result
          }else
          {
            const {data:result}=await this.$http.put("/stu/updateMenu",this.addMenuform)
            res=result
          }
          if (res.code!==0) return this.$message.error("保存菜单失败:"+res.msg); 
          
          this.$message.success("保存菜单成功")
          this.getMenus()
      })
      this.dialogVisible = false
    },
    async delMenu(row){
       const {data:res}=await this.$http.delete("/stu/deleteMenu",  { data:{"id":row.ID}})
          if (res.code!==0) return this.$message.error("删除菜单失败:"+res.msg); 
          
          this.$message.success("删除菜单成功")
          this.getMenus()
    },
    updateMenu(row){
      this.disabled=true
      this.addMenuform.parentId=row.parentId
      this.addMenuform.path=row.path
      this.addMenuform.name=row.name
      this.addMenuform.id=row.ID
      this.dialogVisible=true
    }
  }
}
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.el-card {
  box-shadow: 0 1px 1px;
}
.card-bottom{
    margin-bottom: 10px;
    padding-left: 40%;
}
.el-table{
  margin-top:10px;
}
</style>